package dcdroplet;

// Import Java JDK Classes
import java.util.Set;
import java.util.regex.PatternSyntaxException;

/**
 * General methods for use by DCDroplet.<br/>
 * @author dca
 */
public class Util {
	private Util() {}

	/**
	 * Iterates through a set of tags, and checks if the supplied tag is contained therein.<br/>
	 * This will check the tag insensitive to case, and will also check if it matches via regular expression.<br/>
	 * @param tags The set of tags to check against.<br/>
	 * @param tag The tag to check against the set of tags.<br/>
	 * @return true or false<br/>
	 */
	static boolean containsTag(Set<String> tags, String tag) {
		return containsTag(tags, tag, false, true);
	} // END containsTag

	/**
	 * Iterates through a set of tags, and checks if the supplied tag is contained therein.<br/>
	 * Optionally, the user can specify if the tag should be checked against the set items case-sensitively and/or by regular expression.<br/>
	 * @param tags The set of tags to check against.<br/>
	 * @param tag The tag to check against the set of tags.<br/>
	 * @param caseSensitive If true, then the tag can only be matched if the string case is the same.
	 * @param checkRegex If true, then the tag should also be tried as a regular expression as well.
	 * @return true or false<br/>
	 */
	static boolean containsTag(Set<String> tags, String tag, boolean caseSensitive, boolean checkRegex) {
		// Only allow valid parameters
		if(null == tags || null == tag) {
			return false; // If invalid parameters, a match cannot be found
		} // END if

		// For each tag in the set of tags
		for(String setTag : tags) {
			try {
				if(caseSensitive && tag.equals(setTag)) {
					return true; // Matched tag by string value
				} else if(!caseSensitive && tag.equalsIgnoreCase(setTag)) {
					return true; // Matched tag by string value
				} else if(checkRegex && tag.matches(setTag)) {
					return true; // Matched tag by regular expression
				} else if(checkRegex && !caseSensitive && tag.toLowerCase().matches(setTag.toLowerCase())) {
					return true; // Matched tag by regular expression
				} else {
					continue;
				} // END if/else
			} catch(PatternSyntaxException patternExc) {
				continue; // In case there was a malformed regex
			} // END try/catch
		} // END loop

		return false; // If we've finished looping through the set of tags, then a match could not be found
	} // END containsTag
} // END class Util
